{% extends 'al120/base.html' %}
{% block content %}
    <div class="row">
        <h3>铝成分填写</h3>
    </div>
    <div class="row">
        <div class="col-auto" style="margin-top: 10px; margin-bottom: 10px">
            <span class="badge bage-pill badge-danger">本页面OCR识别，需要核对后，再提交</span>
        </div>
    </div>
    <form id="ff">
        {% csrf_token %}
        <div class="form-group row">
            <label for="id_time" class="col-sm-2 col-form-label">检验时间</label>
            <input type="datetime-local" name="sample_time" class="form-control col-sm-6" id="id_time"
                   required>
        </div>
        <div class="form-group row">
            <label for="'id_source" class="col-sm-2 col-form-label">样块出处</label>
            <input type="text" name="sample_source" class="form-control col-sm-6" placeholder="" id="'id_source"
                   required>
        </div>
        <div class="form-group row">
            <label for="'id_si" class="col-sm-2 col-form-label">1 Si</label>
            <input type="number" name="si" class="form-control col-sm-6" placeholder="填整数，如7516代表7.516" id="'id_si"
                   required value="{{ text.0 }}">
        </div>
        <div class="form-group row">
            <label for="'id_fe" class="col-sm-2 col-form-label">2 Fe</label>
            <input type="number" name="fe" class="form-control col-sm-6" placeholder="" id="'id_fe"
                   value="{{ text.1 }}">
        </div>
        <div class=" form-group row">
            <label for="'id_cu" class="col-sm-2 col-form-label">3 Cu</label>
            <input type="number" name="cu" class="form-control col-sm-6" placeholder="" id="'id_cu"
                   value="{{ text.2 }}">
        </div>
        <div class=" form-group row">
            <label for="'id_mn" class="col-sm-2 col-form-label">4 Mn</label>
            <input type="number" name="mn" class="form-control col-sm-6" placeholder="" id="'id_mn" min="0.00"
                   max="1.0" step="0.0001" value="{{ text.3 }}">
        </div>
        <div class="form-group row">
            <label for="'id_mg" class="col-sm-2 col-form-label">5 Mg</label>
            <input type="number" name="mg" class="form-control col-sm-6" placeholder="" id="'id_mg"
                   value="{{ text.4 }}">
        </div>
        <div class="form-group row">
            <label for="'id_zn" class="col-sm-2 col-form-label">6 Zn</label>
            <input type="number" name="zn" class="form-control col-sm-6" placeholder="" id="'id_zn"
                   value="{{ text.5 }}">
        </div>
        <div class="form-group row">
            <label for="'id_ti" class="col-sm-2 col-form-label">7 Ti</label>
            <input type="number" name="ti" class="form-control col-sm-6" placeholder="" id="'id_ti"
                   value="{{ text.6 }}">
        </div>
        <div class="form-group row">
            <label for="'id_ca" class="col-sm-2 col-form-label">8 Ca</label>
            <input type="number" name="ca" class="form-control col-sm-6" placeholder="" id="'id_ca"
                   value="{{ text.7 }}">
        </div>
        <div class="form-group row">
            <label for="'id_cr" class="col-sm-2 col-form-label">Cr</label>
            <input type="number" name="cr" class="form-control col-sm-6" placeholder="" id="'id_cr"
                   value="{{ text.8 }}">
        </div>
        <div class="form-group row">
            <label for="'id_sb" class="col-sm-2 col-form-label">Sb</label>
            <input type="number" name="sb" class="form-control col-sm-6" placeholder="" id="'id_sb"
                   value="{{ text.9 }}">
        </div>
        <div class="form-group row">
            <label for="'id_sr" class="col-sm-2 col-form-label">Sr</label>
            <input type="number" name="sr" class="form-control col-sm-6" placeholder="" id="'id_sr">
        </div>
        <div class="form-group row">
            <label for="'id_al" class="col-sm-2 col-form-label">Al</label>
            <input type="number" name="al" class="form-control col-sm-6" placeholder="" id="'id_al">
        </div>
        <button type="submit" class="btn btn-info" id="id_submit">提交</button>
        <button type="reset" class="btn btn-info">重置</button>
    </form>

{% endblock %}
{% block script %}
    <script>
        $(document).ready(function () {
            let csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();

            function csrfSafeMethod(method) {
                // these HTTP methods do not require CSRF protection
                return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
            }

            $.ajaxSetup({
                beforeSend: function (xhr, settings) {
                    if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                        xhr.setRequestHeader("X-CSRFToken", csrftoken);
                    }
                }
            });
            // 查询日期默认今天
            let dt = moment().format('YYYY-MM-DDTHH:mm');
            $('#id_time').val(dt);

            $('#id_submit').click(function (evt) {
                evt.preventDefault();
                $.post(
                    '{% url "al120:create" %}',
                    $('#ff').serialize(),
                    function (data) {
                        if (data.status) {
                            alert('保存成功');
                            $('.input').val('');
                        } else {
                            alert(data.err);
                        }
                    }
                );
            });
        });
    </script>

{% endblock %}